"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : 调用JSON  #>> 操作符时左操作数无限嵌套是否会造成缓冲区溢出
Create At   : 2023/06/05
@zou_jialiang0501162244
Description :
    1. 调用 #>> 操作符 左操作数 container-json 无限嵌套
Expect      :
    1. 成功，不会出现缓冲区溢出
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Overflow(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.cons = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')

    def test_security(self):
        text1 = '-----step1:调用 #>> 操作符 左操作数 container-json 无限嵌套;' \
                'expect:成功，不会出现缓冲区溢出-----'
        self.log.info(text1)
        num = 8000
        left_brackets = num * '['
        right_brackets = num * ']'
        val = '0' * num
        sql = f'''select '{left_brackets}1{right_brackets}'::json #>>'''
        sql += "'{{" + val + "}}';"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(f'[1]', msg, f'执行失败:{text1}')
        self.assertTrue(msg.find('column') > -1, f'执行失败:{text1}')

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
